#!/bin/sh
#  Copyright (C) 2007  Sean Busbey, Roman Garnett, Brad Skaggs, Paul Ostazeski
#  
#  This program is free software: you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation, either version 3 of the License, or (at your option)
#  any later version.
#  
#  This program is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
#  more details.
#  
#  You should have received a copy of the GNU General Public License along with
#  this program.  If not, see <http://www.gnu.org/licenses/>.
#  

home=`pwd`
map="${home}/worlds/tiles"
serverLog="${home}/Server.log"
serverPid="${home}/Server.pid"
displayLog="${home}/Display.log"

# Start the server
pushd server > /dev/null 2>&1
if [ ! -f "Server.class" ]; then
   echo "Compiling the server..."
   javac Server.java
fi
echo -n "Starting the server.."
java Server -b "${map}" > "${serverLog}" 2>&1 &
svrPid=$!
echo ${svrPid} > "${serverPid}"

sleep 5
# Give the server a little time to start...
while [ `grep 'Starting display ' "${serverLog}" > /dev/null` ]; do
   echo -n "."
   sleep 2
done
echo ". pid ${svrPid}"
popd > /dev/null 2>&1

# Start the display
pushd display > /dev/null 2>&1
echo -n "Starting the display..."
./display.rb > "${displayLog}" 2>&1 &
dspPid=$!

while [ ! -s "${displayLog}" ]; do
   echo -n "."
   sleep 1
done
echo ". pid ${dspPid}"
popd > /dev/null 2>&1

# Attach agents
pushd agents/java > /dev/null 2>&1
if [ ! -f "Agent.class" ]; then
   echo "Compiling agents..."
   javac Agent.java
   javac SmartBugMover.java
   javac SmartHunterMover.java
fi

for idx in 1 ; do
   echo "Starting bug agent ${idx}..."
   java Agent localhost 7331 SmartBugMover P '[1-9]'	\
   > Bug${idx}.log 2>&1 &
   #> /dev/null 2>&1 &	
done

for idx in 1 2 3 4; do
   echo "Starting hunter agent ${idx}..."
   java Agent localhost 1337 SmartHunterMover '[B-N]'	\
   > Hunter${idx}.log 2>&1 &
   #> /dev/null 2>&1 &	
done
popd > /dev/null 2>&1


echo "-------------------------------------------------"
echo "Press any key to terminate the server and display"
read key

kill ${dspPid}
kill ${svrPid}
rm -f "${serverPid}"
